function testAPI() {
  console.log('Fetching your information.... ');
  FB.api('/me', function(response) {
    console.log('Good to see you, ' + response.name + '.');
  });
}

// how many Facebook messages we have to display
window.nbPosts = 8;
window.nbShownPosts = 8;
$("#nbposts-slider")[0].value=window.nbShownPosts;

function displayPosts(collection) {
  console.log(collection);
  for (i=0;i<collection.length;i++) {
    console.log(collection.get(i));
  }
}

// Facebook fetching time interval in seconds
window.fbCheckInterval = 30;

// time interval between each Facebook fetching
window.refreshTime = 30;

// show friend links between posts
window.showLinks = 0;

// enable audio
window.enableSound = 0;

// cut the message to get a complete sentence
function cutMessage(message) {
  for (i=message.length-1; i>0; i--) {
    if (message[i]==='.' || message[i]==='?' || message[i]==='!')
      return message.substr(0,i+1);
  }
  return message;
}

// create a new FBPost from the Facebook response.data
function createFBPost(fbData) {
  // create a new post
  var post = new FBPost();
  // set the post
  if (fbData.id)
    post.set({id: fbData.id});
  if (fbData.message) {
    if(fbData.message.length>100)
      post.set({message: cutMessage(fbData.message.substr(0,101))+' [...]'});
    else
      post.set({message: fbData.message});
  }
  else if (fbData.story)
    post.set({message: fbData.story});
  if (fbData.created_time)
    post.set({time: fbData.created_time});
  if (fbData.likes)
    post.set({likes: fbData.likes.count});
  if (fbData.comments)
    post.set({comments: fbData.comments.count});
  if (fbData.from.id)
    post.set({from_id: fbData.from.id});
  if (fbData.from.name) {
    post.set({from_name: fbData.from.name});
  }
  if (fbData.from.category){
    post.set({from_page: 1});
  }
  // different request for the profile picture
  FB.api('/'+fbData.from.id+'/picture?type=large', 'get', function(response) {
    post.set({from_picture: response.data.url});
  });
  // define a random position
  post.set({pos_x: Math.floor(Math.random()*71) +10});
  post.set({pos_y: Math.floor(Math.random()*71) +10});
  return post;
}

// Facebook request to download the 25 latest Facebook news feed posts
function checkFb() {
  console.log('Fetching last messages... ');
  FB.api('/me/home', 'get', function(response) {
    if (!response || response.error) {
      console.log(response.error);
    } else {
      for (var i=0; i<nbPosts; i++) {
        var post = createFBPost(response.data[i]);
        // check if the post is already in the list
        if (fbPosts.isIncluded(post)) {
          // check if the number of likes/comments has been modified
          if (fbPosts.get(post.id).likes != post.likes)
            fbPosts.get(post.id).set({likes: post.likes});
          if (fbPosts.get(post.id).comments != post.comments)
            fbPosts.get(post.id).set({likes: post.comments});
        }
        else {
          if (fbPosts.length>=nbPosts) {
            console.log("Removing " + fbPosts.getOlder().get('from_name'));
            fbPosts.remove(fbPosts.getOlder());
          }
          console.log("Adding " + post.get('from_name'));
          fbPosts.add(post);
        }
      }
    }
  });
}

// delete the post collection
function deleteCollection(collection) {
  for (i=0;i<collection.length;i++) {
    collection.remove(collection.models[0]);
  }
}

// check Facebook news feed every <interval> seconds
function checkFbEvery(interval) {
  window.fbCheckInterval = setInterval(function() {
    if (window.enableFBChecking==1)
      checkFb();
  }, interval*1000);
}

window.samples = [
    { id:"01", message:"I'm trying to make the world a more open place.", likes:35, comments:0,
      from_name:"Mark Zuckerberg", from_picture:"img/test-pic1.jpg", pos_x:"54", pos_y:"47"},
    { id:"02", message:"The Arc of the moral universe is long, but it bends towards justice.", likes:25, comments:4,
      from_name:"Barack Obama", from_picture:"img/test-pic2.jpg", pos_x:"12", pos_y:"59"},
    { id:"03", message:"Le changement c'est maintenant.", likes:56, comments:15,
      from_name:"François Hollande", from_picture:"img/test-pic3.jpg", pos_x:"35", pos_y:"53"},
    { id:"04", message:"I'm lovin' it.", likes:15, comments:0,
      from_name:"McDonald's", from_picture:"img/test-pic4.jpg", from_page:1, pos_x:"76", pos_y:"46"},
    { id:"05", message:"Mr Bean shared a link.", likes:6, comments:0,
      from_name:"Mr Bean", from_picture:"img/test-pic5.jpg", pos_x:"5", pos_y:"33"},
    { id:"06", message:"Maintenant en DVD et Blu-Ray.", likes:8, comments:0,
      from_name:"The Dark Knight Rises", from_picture:"img/test-pic6.jpg", from_page:1, pos_x:"67", pos_y:"35"},
    { id:"07", message:"On en a gros !", likes:2, comments:13,
      from_name:"Sir Perceval", from_picture:"img/test-pic7.jpg", pos_x:"78", pos_y:"67"},
    { id:"08", message:"Vous pouvez répéter la question ?", likes:4, comments:5,
      from_name:"Alain Deloin", from_picture:"img/test-pic8.jpg", pos_x:"16", pos_y:"18"}
  ];

// manage the demo mode
function startDemo() {
  deleteCollection(fbPosts);
  for(i=0;i<nbPosts;i++) {
    fbPosts.add(samples[i]);
  }
}

//startDemo();